package com.taptech.doufu.services;

import android.content.Context;
import android.content.Intent;

import com.taptech.doufu.ui.activity.NewFullScreenImageActivity;
import com.taptech.doufu.ui.activity.NewPersonalNoteDesActivity;
import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.ui.activity.SweepFlowerActivity;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpUtil;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by lsy on 2015/7/2.
 */
public class PersonalNoteService
{

    public static final String BASE_URL = "http://" + Constant.HOST_NAME + "/index.php/";

    public static final String OBJECT_TYPE_NOTE = "41";
    public static final String OBJECT_TYPE_DRAW = "42";
    public static final String OBJECT_TYPE_COSER = "43";
    public static final int HANDLE_TYPE_LOAD_NOTE = 1042;
    public static final int HANDLE_TYPE_LOAD_SINGLE_NOTE = 1043;

    public static final int HANDLE_TYPE_LOAD_FLOWER_LIST = 1044;
    public static final int HANDLE_TYPE_GIVE_FLOWER = 1045;
    public static final int HANDLE_TYPE_LOAD_COMMENT_LIST = 1046;
    public static final int HANDLE_TYPE_SEND_COMENT = 1047;
    public static final int HANDLE_TYPE_JUDGE_COLLECTION_EXIST = 1048;
    public static final int HANDLE_TYPE_ADD_COLLECT = 1049;
    public static final int HANDLE_TYPE_REMOVE_COLLECT = 1050;

    public static final int HANDLE_TYPE_DELETE_NOTE = 1051;

    public static final int HANDLE_TYPE_IS_NEED_COMPETENCE_WINDOW = 1052;
    public static final int HANDLE_TYPE_GIVE_FLOWER_FAST = 1053;



    private static PersonalNoteService instance = new PersonalNoteService();

    private PersonalNoteService()
    {
    }

    public static PersonalNoteService getInstance()
    {
        return instance;
    }


    //进入日志详情 object_type: 41——日志      42——绘画       43——COSER
    static public void enterPersonalNoteDesActivity(Context context, String id, String object_type)
    {
        Intent intent = new Intent();
        int objectType = 0;
        try
        {
            objectType = Integer.valueOf(object_type);
        } catch (Exception e)
        {

        }
        switch (objectType)
        {
            case Constant.OBJECT_TYPE_NOTE:
                intent.setClass(context, SimpleWeexActivity.class);
                intent.putExtra(SimpleWeexActivity.INTENT_WEEX_URL_KEY,"vue/find/TopicDetail/TFTopicDetail.js");
                HashMap<String, String> data = new HashMap<>();
                data.put("id",id);
                intent.putExtra(SimpleWeexActivity.INTENT_WEEX_DATA_KEY,data);
                context.startActivity(intent);
                break;
            case Constant.OBJECT_TYPE_DRAW:
            case Constant.OBJECT_TYPE_COS:
                intent.setClass(context, NewPersonalNoteDesActivity.class);
                intent.putExtra(Constant.ARTICLE_ID, id);
                intent.putExtra(Constant.OBJECT_TYPE, object_type);
                context.startActivity(intent);
                break;
            case Constant.OBJECT_TYPE_NOVEL:
                NovelDetailsActivity.Companion.startActivity(context,id,Intent.FLAG_ACTIVITY_NEW_TASK);
                break;
            case Constant.OBJECT_TYPE_SWEEP:
                intent.setClass(context, SweepFlowerActivity.class);
                intent.putExtra(Constant.SWEEP_ID, id);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                break;

        }


    }



    public void enterPersonalNoteBigImage(Context context, LinkedHashSet<String> imageSet, LinkedHashSet<String> originalImageSet, int position, ArrayList<Integer> imageWidthList, ArrayList<Integer> imageHeightList, boolean ableToDownLoad)
    {
        Intent intent = new Intent(context, NewFullScreenImageActivity.class);
        intent.putExtra(Constant.LIST, imageSet);
        intent.putExtra(Constant.IMAGE, originalImageSet);
        intent.putExtra(Constant.ARTICLE_ITEM, position);
        intent.putExtra(Constant.ABLE_TO_DOWNLOAD, ableToDownLoad);
        intent.putExtra(Constant.HEIGHT, imageHeightList);
        intent.putExtra(Constant.WIDTH, imageWidthList);
        context.startActivity(intent);
    }


    /**
     * 获取某个人日志信息
     */
    //41:日志   42:绘画   43:COSER
    public void loadPersoanlNoteListByUser(HttpResponseListener listener, String last, String user_id, String object_type)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_NOTE);
        request.setUrl(BASE_URL + "journals/mine?" + "last=" + last + "&user_id=" + user_id + "&object_type=" + object_type);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    /**
     * 获取某篇日志详情
     */
    public void loadPersonalNoteDes(HttpResponseListener listener, String id)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_SINGLE_NOTE);
        request.setUrl(BASE_URL + "journals/one?id=" + id);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }





    /**
     * 获取献花列表
     *
     * @param listener
     * @param id
     * @param object_type OBJECT_TYPE_NOVEL_REVIEW(29)， _DRAWING(42), _COSPLAY(43), JOURNAL(41)
     * @param last
     */
    public void loadFlowerList(HttpResponseListener listener, String id, String object_type, String last)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_FLOWER_LIST);
        request.setUrl(BASE_URL + "flower/flower_users?id=" + id + "&object_type=" + object_type + "&size=" + Constant.PAGE_SIZE_6 + "&last=" + last);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    public void loadFlowerList(HttpResponseListener listener, String id, String object_type, String last, String size)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_FLOWER_LIST);
        request.setUrl(BASE_URL + "flower/flower_users?id=" + id + "&object_type=" + object_type + "&size=" + size + "&last=" + last);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    /**
     * 获取评论列表
     *
     * @param listener
     * @param target_id
     * @param target_type OBJECT_TYPE_NOVEL_REVIEW(29)， _DRAWING(42), _COSPLAY(43), JOURNAL(41)
     * @param last
     */
    public void loadCommentList(HttpResponseListener listener, String target_id, String target_type, String last)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_COMMENT_LIST);
        request.setUrl(BASE_URL + "comment/index2?target_id=" + target_id + "&target_type=" + target_type + "&size=" + Constant.PAGE_SIZE_10 + "&last=" + last);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    /**
     * 献花
     *
     * @param listener
     * @param id
     * @param object_type OBJECT_TYPE_NOVEL_REVIEW(29)， _DRAWING(42), _COSPLAY(43), JOURNAL(41)
     * @param num         献花数
     */

    public void giveFlowerToNote(HttpResponseListener listener, String id, String object_type, String num)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_GIVE_FLOWER);
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("object_type", object_type);
        list.add(value);
        value = new BasicNameValuePair("id", id);
        list.add(value);
        value = new BasicNameValuePair("num", num);
        list.add(value);
        request.setData(list);
        request.setUrl(BASE_URL + "flower/give");
        request.setListener(listener);
        HttpUtil.sendPostRequest(request);
    }

    public void giveFlowerToNote(HttpResponseListener listener, String id, String object_type, String num,int fast_option_id)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_GIVE_FLOWER_FAST);
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("object_type", object_type);
        list.add(value);
        value = new BasicNameValuePair("id", id);
        list.add(value);
        value = new BasicNameValuePair("num", num);
        list.add(value);
        value = new BasicNameValuePair("fast_option_id", String.valueOf(fast_option_id));
        list.add(value);
        request.setData(list);
        request.setUrl(BASE_URL + "flower/give");
        request.setListener(listener);
        HttpUtil.sendPostRequest(request);
    }

    /**
     * 发表评论
     *
     * @param listener
     * @param target_id
     * @param target_type     OBJECT_TYPE_NOVEL_REVIEW(29)， _DRAWING(42), _COSPLAY(43), JOURNAL(41)
     * @param comment_content 评论内容
     */
    public void sendCommentToNote(HttpResponseListener listener, String target_id, String target_type, String comment_content)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_SEND_COMENT);
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("target_type", target_type);
        list.add(value);
        value = new BasicNameValuePair("target_id", target_id);
        list.add(value);
        value = new BasicNameValuePair("comment_content", comment_content);
        list.add(value);
        request.setData(list);
        request.setUrl(BASE_URL + "comment/post2");
        request.setListener(listener);
        HttpUtil.sendPostRequest(request);
    }

    /*          收藏模块           */

    public void judgeCollectExist(HttpResponseListener listener, String id, String object_type)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_JUDGE_COLLECTION_EXIST);
        request.setUrl(BASE_URL + "favorite/has_add?id=" + id + "&object_type=" + object_type);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    public void addCollectToNote(HttpResponseListener listener, String id, String object_type)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_ADD_COLLECT);
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("object_type", object_type);
        list.add(value);
        value = new BasicNameValuePair("id", id);
        list.add(value);
        request.setData(list);
        request.setUrl(BASE_URL + "favorite/add");
        request.setListener(listener);
        HttpUtil.sendPostRequest(request);
    }

    public void removeCollectTONote(HttpResponseListener listener, String id, String object_type)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_REMOVE_COLLECT);
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("object_type", object_type);
        list.add(value);
        value = new BasicNameValuePair("id", id);
        list.add(value);
        request.setData(list);
        request.setUrl(BASE_URL + "favorite/remove");
        request.setListener(listener);
        HttpUtil.sendPostRequest(request);
    }

    public void deletePersonalNote(HttpResponseListener listener, String id)
    {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_DELETE_NOTE);
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("id", id);
        list.add(value);
        request.setData(list);
        request.setUrl(BASE_URL + "journals/remove");
        request.setListener(listener);
        HttpUtil.sendPostRequest(request);
    }


    public void isNeedPopWindowRequest(HttpResponseListener listener, String object_type) {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_IS_NEED_COMPETENCE_WINDOW);
        request.setUrl(BASE_URL + "requests/need?object_type=" + object_type);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }
}
